Completed
Pull Request — master (#214)
by Sander
04:14
created

$(document).ready   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 3
rs 10
1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
$(document).ready(function () {
24
25
	var Settings = function (baseUrl) {
26
		this._baseUrl = baseUrl;
27
		this._settings = [];
28
	};
29
30
	Settings.prototype = {
31
		load: function () {
32
			var deferred = $.Deferred();
33
			var self = this;
34
			$.ajax({
35
				url: this._baseUrl,
36
				method: 'GET',
37
				async: false
38
			}).done(function (settings) {
39
				self._settings = settings;
40
			}).fail(function () {
41
				deferred.reject();
42
			});
43
			return deferred.promise();
44
		},
45
46
		setUserKey: function (key, value) {
47
			var request = $.ajax({
48
				url: this._baseUrl + '/' + key + '/' + value,
49
				method: 'POST'
50
			});
51
			request.done(function () {
52
				$('.msg-passwords').removeClass("msg_error");
53
				$('.msg-passwords').text('');
54
			});
55
			request.fail(function () {
56
				$('.msg-passwords').addClass("msg_error");
57
				$('.msg-passwords').text(t('passwords', 'Error while saving field') + ' ' + key + '!');
58
			});
59
		},
60
61
		setAdminKey: function (key, value) {
62
			var request = $.ajax({
63
				url: this._baseUrl + '/' + key + '/' + value +'/admin1/admin2',
64
				method: 'POST'
65
			});
66
			request.done(function () {
67
				$('.msg-passwords').removeClass("msg_error");
68
				$('.msg-passwords').text('');
69
			});
70
			request.fail(function () {
71
				$('.msg-passwords').addClass("msg_error");
72
				$('.msg-passwords').text(t('passwords', 'Error while saving field') + ' ' + key + '!');
73
			});
74
		},
75
		getKey: function (key) {
76
			if(this._settings.hasOwnProperty(key)){
77
				return this._settings[key];
78
			}
79
			return false;
80
		},
81
		getAll: function () {
82
			return this._settings;
83
		}
84
	};
85
86
87
	var settings = new Settings(OC.generateUrl('apps/passman/api/v2/settings'));
88
	settings.load();
89
90
	// ADMIN SETTINGS
91
92
	// fill the boxes
93
	$('#passman_link_sharing_enabled').prop('checked', (settings.getKey('link_sharing_enabled').toString().toLowerCase() === '1'));
94
	$('#passman_sharing_enabled').prop('checked', (settings.getKey('user_sharing_enabled').toString().toLowerCase() === '1'));
95
	$('#passman_check_version').prop('checked', (settings.getKey('check_version').toString().toLowerCase() === '1'));
96
	$('#passman_https_check').prop('checked', (settings.getKey('https_check').toString().toLowerCase() === '1'));
97
	$('#passman_disable_contextmenu').prop('checked', (settings.getKey('disable_contextmenu').toString().toLowerCase() === '1'));
98
	$('#passman_disable_debugger').prop('checked', (settings.getKey('disable_debugger').toString().toLowerCase() === '1'));
99
	$('#vault_key_strength').val(settings.getKey('vault_key_strength'));
100
101
102
	$('#passman_check_version').change(function () {
103
		settings.setAdminKey('check_version', ($(this).is(":checked")) ? 1 : 0);
104
	});
105
106
	$('#passman_https_check').change(function () {
107
		settings.setAdminKey('https_check', ($(this).is(":checked")) ? 1 : 0);
108
	});
109
110
	$('#passman_disable_contextmenu').change(function () {
111
		settings.setAdminKey('disable_contextmenu', ($(this).is(":checked")) ? 1 : 0);
112
	});
113
114
	$('#passman_disable_debugger').change(function () {
115
		settings.setAdminKey('disable_debugger', ($(this).is(":checked")) ? 1 : 0);
116
	});
117
118
	$('#passman_sharing_enabled').change(function () {
119
		settings.setAdminKey('user_sharing_enabled', ($(this).is(":checked")) ? 1 : 0);
120
	});
121
122
	$('#passman_link_sharing_enabled').change(function () {
123
		settings.setAdminKey('link_sharing_enabled', ($(this).is(":checked")) ? 1 : 0);
124
	});
125
	$('#vault_key_strength').change(function () {
126
		settings.setAdminKey('vault_key_strength', $(this).val());
127
	});
128
129
	if($('form[name="passman_settings"]').length === 2){
130
		$('form[name="passman_settings"]')[1].remove();
131
	}
132
133
});
134